﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Com.Toook.Common;
using System.Data.SQLite;
using System.Text;

public partial class news_list : BaseShowPage
{
    private int pageSize = 20;
    protected string NewsListHtml = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            InitNewsList();
        }
    }
    private void InitNewsList()
    {
        string type = Request["type"];       
        int p = 1;
        string typeCondition = "";
        //类型不为空且是数字，设置类型条件。否则不加类型条件。
        if (!string.IsNullOrEmpty(type) && int.TryParse(type, out p))
        {
            typeCondition = " and type=" + type;
        }
        else
        {
            type = "";
        }

        DataRow row = PageData.SelectParamRow("typeCode='news" + type + "'");        
        if (row == null)//没有找到，类型不对.设置为默认
        {
            row = PageData.SelectParamRow("typeCode='news'");           
        }
        Master.SetTitleKeyDesc(row);
        //Page.Title = row[PageColumn.title].ToString();
        //HtmlMeta metaDesc = (HtmlMeta)this.Master.FindControl("metaDesc");
        //HtmlMeta metaKey = (HtmlMeta)this.Master.FindControl("metaKey");
        //metaDesc.Content = row[PageColumn.description].ToString();
        //metaKey.Content = row[PageColumn.keyword].ToString();

        StringBuilder sbHtml = new StringBuilder(8192);
        string siteId = row[PageColumn.newsSiteId].ToString();
        string[] ids = siteId.Split(',');
        
        string sqlStart = "select id, siteId, title, case haveContent when 1 then '' else link end as url, pubDate " +
            " from RssNews where isGetContent=1 " + typeCondition;
        string sqlEnd = " order by createTime desc limit 0," + pageSize.ToString();
        SQLiteConnection conn = SqliteHelper.GetConnection("SQLiteDatabase");
        SQLiteCommand cmd = new SQLiteCommand(conn);
        SQLiteDataReader reader = null;
        try
        {
            conn.Open();
            foreach (string sid in ids)
            {
                string sql = sqlStart + " and siteId=" + sid + sqlEnd;
                cmd.CommandText = sql;
                bool isFirst = true;
                try
                {
                    reader = cmd.ExecuteReader();                    
                    while (reader.Read())
                    {
                        if(isFirst)
                        {
                            isFirst = false;
                            DataRow rowSite = PageData.SelectNewsSiteRow(sid);
                            string siteName = rowSite["SiteName"].ToString();
                            sbHtml.Append("<div class=\"newsSite\" id=\"news").Append(sid).Append("\"><p>");
                            sbHtml.Append(siteName).Append("</p><ul>");
                        }
                        //如果有内容，跳转到内容页，否则直接跳转到来源url
                        string url = reader["url"].ToString() != "" ? 
                            " rel=\"nofollow\" href=\"" + reader["url"].ToString() + "\"" : 
                            " href=\"content.aspx?id=" + reader["id"].ToString() + "\"";

                        sbHtml.Append("<li><a target=\"blank\" ").Append(url).Append("\">");
                        sbHtml.Append(reader["title"].ToString()).Append("</a>").Append(reader["pubDate"].ToString()).Append("</li>");
                    }
                    reader.Close();
                    if(!isFirst)//isFirst = false;说明已经读取到数据，加上结束标签
                    {
                        sbHtml.Append("</ul></div>");
                    }
                }
                catch (Exception exRead)
                {
                    LogHelper.Log.Error("读取新闻数据出错。sql=" + sql, exRead);
                }
            }
        }
        finally
        {
            SqliteHelper.CloseDbResource(conn, reader);
        }

        NewsListHtml = sbHtml.ToString();
    }    
}
